package common;

import java.util.HashSet;
import java.util.Hashtable;

import common.Enums.MessageType;

/**
 * @author alex
 * A data or command message passed between the client or server.
 */
public class Message
{
    //
    // Constructor/Methods
    //
	
    /**
     * Default Constructor 
     */
    public Message()
    {
        this._MessageType = null;
        this._Parameters = new Hashtable<String, String>();
        this._Content = null;
    }
	
    /**
     * @param messageType
     *            Constructs a new message of the specified type.
     */
    public Message(MessageType messageType)
    {
        this._MessageType = messageType;
        this._Parameters = new Hashtable<String, String>();
        this._Content = null;
    }

    /**
     * Gets the message type for this message.
     * 
     * @return The message type.
     */
    public MessageType GetMessageType()
    {
        return this._MessageType;
    }

    /**
     * Sets the message type for this message.
     * 
     * @param messageType
     *            The message type.
     */
    public void SetMessageType(MessageType messageType)
    {
        this._MessageType = messageType;
    }

    /**
     * Gets the content for this message. Returns null if the message has no
     * content.
     * 
     * @return The message content.
     */
    public String GetContent()
    {
        return this._Content;
    }

    /**
     * Sets the content for this message. For messages with no content, null
     * should be used.
     * 
     * @param content
     *            The message content.
     */
    public void SetContent(String content)
    {
        this._Content = content;
    }
    
    /**
     * Gets a set of all parameters present in this message.
     * @return The list of parameters.
     */
    public HashSet<String> GetAllParameters()
    {
        return new HashSet<String>(this._Parameters.keySet());
    }

    /**
     * Gets the value of the parameter with the specified name, or null, if it 
     * is not present.
     * 
     * @param parameterName
     *            The name of the parameter whose value to retrieve.
     * @return The value of the parameter.
     */
    public String GetParameter(String parameterName)
    {
        if (this._Parameters.containsKey(parameterName))
        {
            return (String) this._Parameters.get(parameterName);
        }
        return null;
    }

    /**
     * Sets the parameter with the specified name to the specified value. If the
     * parameter does not already exist in the message, it is added. If it does
     * exist, it is overwritten.  Parameter names will be set to lower case.
     * 
     * @param parameterName
     *            The name of the parameter to set.
     * @param parameterValue
     *            The value to set the parameter to.
     */
    public void SetParameter(String parameterName, String parameterValue)
    {
        this._Parameters.put(parameterName.toLowerCase(), parameterValue);
    }

    /**
     * Removes the parameter with the specified name from the message.
     * 
     * @param parameterName
     *            The name of the parameter to remove.
     */
    public void RemoveParameter(String parameterName)
    {
        this._Parameters.remove(parameterName);
    }

    /**
     * Removes all of the parameters.
     */
    public void ClearParameters()
    {
        this._Parameters.clear();
    }

    //
    // Members
    //

    private MessageType _MessageType = MessageType.NULL;

    private Hashtable<String, String> _Parameters = null;

    private String _Content = null;
}